//
//    Вычисление определителя матрицы
////

public class Determinant {
    static double determinant(double[][] mat) {
        if(mat.length == 1)
            return mat[0][0];

        if(mat.length == 2)
            return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];

        double result = 0;
        for(int i = 0; i < mat.length; i++) {
            double temp[][] = new double[mat.length - 1][mat.length - 1];

            for(int j = 1; j < mat.length; j++) {
                System.arraycopy(mat[j], 0, temp[j-1], 0, i);
                System.arraycopy(mat[j], i+1, temp[j-1], i, mat.length-i-1);
            }

            result += mat[0][i] * Math.pow(-1, i) * determinant(temp);
        }
        return result;
    }
    
    static void printMatrix(double[][] matrix) {
        for (int j = 0; j < matrix.length; j++) {
            for (int i = 0; i < matrix[j].length; i++)
                System.out.printf("%5.2g", matrix[j][i]);
            System.out.println();
        }
    }

    public static void main(String[] args) {
        double[][] matrix = new double[][]
                {
                        {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, -1}
                };

        printMatrix(matrix);
        
        double det = determinant(matrix);
        System.out.println("Determinant = " + det);
    }
}
